package org.gwtrpc4j;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.Date;

import junit.framework.Assert;

import org.gwtrpc4j.http.jse.CookieManager;
import org.gwtrpc4j.http.jse.JSERequestBuilderFactory;
import org.gwtrpc4j.test.app.client.ExceptionGTO;
import org.gwtrpc4j.test.app.client.GreetingService;
import org.gwtrpc4j.test.app.client.GreetingServiceAsync;
import org.gwtrpc4j.test.app.client.RequestGTO;
import org.gwtrpc4j.test.app.client.ResponseGTO;
import org.junit.Before;
import org.junit.Test;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Start the server firt and run the test
 * 
 * @author npeters
 * 
 */
public class JRemoteServiceProxyTestIT {

	String moduleBaseURL = "http://localhost:8888/remote_gwt/";
	String serializationPolicyStrongName = "0A75DAB5498C003CD638DBFFC63FC792";
	String remoteServiceRelativePath = "greet";

	@Test
	public void testSyncro() throws InterruptedException {
		JSERequestBuilderFactory factory = new JSERequestBuilderFactory();
		factory.setTimeoutMillis(5000);
		// disable proxy
		factory.setProxy(new Proxy(Type.HTTP, InetSocketAddress
				.createUnresolved("localhost", 9999)));
		final GreetingService gs = RpcServiceProxyFactory.create(
				GreetingService.class, moduleBaseURL,
				remoteServiceRelativePath, serializationPolicyStrongName,
				factory);
		String firstTime = gs.greetServer("Nicolas");
		Assert.assertEquals(
				"Hello, Nicolas!<br><br>I am running. newSession:true",
				firstTime);
		// seconde time, the session is keeping !
		String secondeTime = gs.greetServer("Nicolas");
		Assert.assertEquals("test session",
				"Hello, Nicolas!<br><br>I am running. newSession:false",
				secondeTime);

		String session = CookieManager.getThreadInstance().getCookie(
				"localhost", "JSESSIONID");
		Assert.assertTrue(session != null);
		CookieManager.getThreadInstance().clear();

		Assert.assertNull(CookieManager.getThreadInstance().getCookie(
				"localhost", "JSESSIONID"));

		CookieManager.getThreadInstance().addCookie("localhost", "JSESSIONID",
				session);

		String oneMore = gs.greetServer("Nicolas");
		Assert.assertEquals("test session",
				"Hello, Nicolas!<br><br>I am running. newSession:false",
				oneMore);

	}

	@Test
	public void testSyncroComplexObject() throws InterruptedException {
		JSERequestBuilderFactory factory = new JSERequestBuilderFactory();
		factory.setTimeoutMillis(5000);
		// disable proxy
		// factory.setProxy(new Proxy(Type.HTTP, InetSocketAddress
		// .createUnresolved("localhost", 9999)));
		final GreetingService gs = RpcServiceProxyFactory.create(
				GreetingService.class, moduleBaseURL,
				remoteServiceRelativePath, serializationPolicyStrongName,
				factory);

		RequestGTO r = new RequestGTO();
		r.date = new Date(10000);
		r.name = "\u0002Test\f";
		ResponseGTO result;
		try {
			result = gs.greetServer(r);
			System.out.println(result);
		} catch (ExceptionGTO e) {
			e.printStackTrace();
			Assert.fail(e.getMessage());
		}
	}

	boolean ok;

	@Before
	public void beforeTest() {
		CookieManager.getThreadInstance().clear();
	}

	@Test
	public void testAsyncro() {

		ok = false;
		GreetingServiceAsync gsAsync = RpcServiceProxyFactory.create(
				GreetingServiceAsync.class, moduleBaseURL,
				remoteServiceRelativePath, serializationPolicyStrongName,
				new JSERequestBuilderFactory());

		gsAsync.greetServer("Nicolas", new AsyncCallback<String>() {

			@Override
			public void onSuccess(String result) {
				Assert.assertEquals(
						"Hello, Nicolas!<br><br>I am running. newSession:true",
						result);
				ok = true;
				return;
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				Assert.fail(caught.getMessage());
			}
		});
		try {
			Thread.currentThread();
			Thread.sleep(1000);
			Assert.assertEquals(true, ok);
		} catch (InterruptedException e) {
			e.printStackTrace();
			Assert.fail(e.getMessage());
		}

	}
}
